package com.css.fxfzypg.modules.maps.controller;

import com.css.fxfzypg.base.response.RestResponse;
import com.css.fxfzypg.modules.maps.queryParams.LonLatsStringQueryParams;
import com.css.fxfzypg.modules.maps.service.SetEarthquakeMapService;
import com.css.fxfzypg.modules.resultupload.repository.entity.ResultMainEntity;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * @author zyt
 * @create 2022-10-18
 * 设定地震地图数据模块
 */
@RestController
@RequestMapping("/earthquakeLossesEstimation/earthquakeSetting")
@Slf4j
public class SetEarthquakeController {

    @Resource
    SetEarthquakeMapService setEarthquakeMapService;//地图使用
    
    /*zyt修改--start*/
    /**
     * 根据任务ID查询任务区域行政区划code列表
     */
    @GetMapping("/getAllCodeListByTaskId/{taskId}")
    public RestResponse getAllCodeListByTaskId(@PathVariable("taskId") String taskId){

        RestResponse restResponse = null;
        try{
            restResponse = RestResponse.succeed(setEarthquakeMapService.getAllCodeListByTaskId(taskId));
        }catch (Exception e){
            log.error("查询数据失败!",e);
            restResponse = RestResponse.fail("查询数据失败");
        }
        return restResponse;
    }

    /**
     * 根据1个任务地区查询行政区划code
     */
    @GetMapping("/getCodeByTaskDistrict/{taskDistrict}")
    public RestResponse getCodeByTaskDistrict(@PathVariable("taskDistrict") String taskId){

        RestResponse restResponse = null;
        try{
            restResponse = RestResponse.succeed(setEarthquakeMapService.getCodeByTaskDistrict(taskId));
        }catch (Exception e){
            log.error("查询数据失败!",e);
            restResponse = RestResponse.fail("查询数据失败");
        }
        return restResponse;
    }

    /*
    *根据任务ID获取任务区域内的相交的断层信息
    */
    @GetMapping("/getIntersectionFaultByTaskId")
    public RestResponse getIntersectionFaultByTaskId(@RequestParam String taskId){
        RestResponse restResponse=null;
        try {
            restResponse=RestResponse.succeed(setEarthquakeMapService.getIntersectionFaultByTaskId(taskId));
        }catch (Exception e){
            log.error("查询数据失败!",e);
            restResponse = RestResponse.fail("查询数据失败");
        }
        return restResponse;
    }

    /*
    *判断经纬度是否在任务区域内部（不包括边线）
    */
    @GetMapping("/isTaskRegionContainsLonLat")
    public RestResponse isTaskRegionContainsLonLat(HttpServletRequest request){
        RestResponse restResponse=null;
        try {
            String taskId = request.getParameter("taskId");
            String lonlat = request.getParameter("lonlat");
            restResponse=RestResponse.succeed(setEarthquakeMapService.isTaskRegionContainsLonLat(taskId,lonlat));
        }catch (Exception e){
            log.error("查询数据失败!",e);
            restResponse = RestResponse.fail("查询数据失败");
        }
        return restResponse;
    }

    /*
     *判断经纬度是否在任务区域边线上（不包括区域内部）
     */
    @GetMapping("/isTaskRegionTouchesLonLat")
    public RestResponse isTaskRegionTouchesLonLat(HttpServletRequest request){
        RestResponse restResponse=null;
        try {
            String taskId = request.getParameter("taskId");
            String lonlat = request.getParameter("lonlat");
            restResponse=RestResponse.succeed(setEarthquakeMapService.isTaskRegionTouchesLonLat(taskId,lonlat));
        }catch (Exception e){
            log.error("查询数据失败!",e);
            restResponse = RestResponse.fail("查询数据失败");
        }
        return restResponse;
    }

    /*
     *根据全部经纬度字符串获取任务区域内的经纬度字符串（多点相交）
     */
    @PostMapping("/getLonLatsStringByTaskId")
    public RestResponse getLonLatsStringByTaskId(@RequestBody LonLatsStringQueryParams obj){
        RestResponse restResponse=null;
        try {
            String taskId = obj.getTaskId();//任务ID
            String lonlatsString = obj.getLonlatsString();//全部经纬度字符串
            restResponse=RestResponse.succeed(setEarthquakeMapService.getLonLatsStringByTaskId(taskId,lonlatsString));
        }catch (Exception e){
            log.error("查询数据失败!",e);
            restResponse = RestResponse.fail("查询数据失败");
        }
        return restResponse;
    }

    /*
     * 根据经纬度字符串查询全部经纬度及行政区划省市区县信息字符串（经度,纬度,省,市,区县;）
     */
    @PostMapping("/queryAllLonLatProvinceCityCountyNameStringByLonLatsString")
    public RestResponse queryAllLonLatProvinceCityCountyNameStringByLonLatsString(@RequestBody LonLatsStringQueryParams obj){
        RestResponse restResponse=null;
        try {
            String taskId = obj.getTaskId();//任务ID
            String lonlatsString = obj.getLonlatsString();//全部经纬度字符串
            restResponse=RestResponse.succeed(setEarthquakeMapService.queryAllLonLatProvinceCityCountyNameStringByLonLatsString(lonlatsString));

        }catch (Exception e){
            log.error("查询数据失败!",e);
            restResponse = RestResponse.fail("查询数据失败");
        }
        return restResponse;
    }

    /*
     * 根据经纬度查询行政区划省市区县信息字符串（省,市,区县）
     */
    @GetMapping("/queryProvinceCityCountyNameStringByLonLat")
    public RestResponse queryProvinceCityCountyNameStringByLonLat(HttpServletRequest request){
        RestResponse restResponse=null;
        try {
            String lon = request.getParameter("lon");
            String lat = request.getParameter("lat");
            restResponse=RestResponse.succeed(setEarthquakeMapService.queryProvinceCityCountyNameStringByLonLat(lon,lat));

        }catch (Exception e){
            log.error("查询数据失败!",e);
            restResponse = RestResponse.fail("查询数据失败");
        }
        return restResponse;
    }

    /**
     * 根据任务ID获取任务范围最小矩形的经纬度值数组
     */
    @GetMapping("/getTaskRegionExtent/{taskId}")
    public RestResponse getTaskRegionExtent(@PathVariable("taskId") String taskId){

        RestResponse restResponse = null;
        try{
            restResponse = RestResponse.succeed(setEarthquakeMapService.getTaskRegionExtent(taskId));
        }catch (Exception e){
            log.error("查询数据失败!",e);
            restResponse = RestResponse.fail("查询数据失败");
        }
        return restResponse;
    }

    /**
     * 获取任务范围的正方形公里格网网格线和震中位置经纬度列表（以右上角为初始点）
     */
    @GetMapping("/getSquareGridCenterLonLatList/{taskId}/{kilometer}")
    public RestResponse getSquareGridCenterLonLatList(@PathVariable("taskId") String taskId,@PathVariable("kilometer") String kilometer){

        RestResponse restResponse = null;
        try{
            restResponse = RestResponse.succeed(setEarthquakeMapService.getSquareGridCenterLonLatList(taskId,kilometer));
        }catch (Exception e){
            log.error("查询数据失败!",e);
            restResponse = RestResponse.fail("查询数据失败");
        }
        return restResponse;
    }

    /*
     *创建地震影响场
     */
    @GetMapping("/createInfluenceEllipse")
    public RestResponse createInfluenceEllipse(HttpServletRequest request){
        RestResponse restResponse=null;
        try {
            String lon = request.getParameter("lon");
            String lat = request.getParameter("lat");
            String long_x = request.getParameter("long_x");
            String short_y = request.getParameter("short_y");
            String angle = request.getParameter("angle");
            String min_long_x = request.getParameter("min_long_x");//内圈长轴
            String min_short_y = request.getParameter("min_short_y");//短圈长轴
            restResponse=RestResponse.succeed(setEarthquakeMapService.createInfluenceEllipse(lon,lat,long_x,short_y,angle,min_long_x,min_short_y));
        }catch (Exception e){
            log.error("查询数据失败!",e);
            restResponse = RestResponse.fail("查询数据失败");
        }
        return restResponse;
    }

    /*
     * 根据taskId，查询全部评估结果列表
     */
    @GetMapping("/getResultTotalByTaskId")
    public RestResponse getResultTotalByTaskId(HttpServletRequest request){
        RestResponse restResponse=null;
        try {
            String taskId = request.getParameter("taskId");
            //查询全部评估结果列表
            List<ResultMainEntity> levelList = setEarthquakeMapService.getResultTotalByTaskId(taskId);

            restResponse=RestResponse.succeed(levelList);

        }catch (Exception e){
            log.error("查询数据失败!",e);
            restResponse = RestResponse.fail("查询数据失败");
        }
        return restResponse;
    }

    /*
     * 根据taskId和序号，查询全部评估结果详情列表
     */
    @GetMapping("/getResultTotalDetailByTaskIdNum")
    public RestResponse getResultTotalDetailByTaskIdNum(HttpServletRequest request){
        RestResponse restResponse=null;
        try {
            String taskId = request.getParameter("taskId");
            String num = request.getParameter("num");
            //查询全部评估结果列表
            List<ResultMainEntity> levelList = setEarthquakeMapService.getResultTotalDetailByTaskIdNum(taskId,num);

            restResponse=RestResponse.succeed(levelList);

        }catch (Exception e){
            log.error("查询数据失败!",e);
            restResponse = RestResponse.fail("查询数据失败");
        }
        return restResponse;
    }

    /*
     * 根据taskId，查询全部设定地震的震级列表
     */
    @GetMapping("/queryInfluencefliedLevelListByTaskId")
    public RestResponse queryInfluencefliedLevelListByTaskId(HttpServletRequest request){
        RestResponse restResponse=null;
        try {
            String taskId = request.getParameter("taskId");
            //震级列表
            List<String> levelList = setEarthquakeMapService.queryInfluencefliedLevelListByTaskId(taskId);

            restResponse=RestResponse.succeed(levelList);

        }catch (Exception e){
            log.error("查询数据失败!",e);
            restResponse = RestResponse.fail("查询数据失败");
        }
        return restResponse;
    }

    /*
     * 根据任务ID、序号，查询震级、角度列表
     */
    @GetMapping("/queryInfluencefliedLevelAngleList")
    public RestResponse queryInfluencefliedLevelAngleList(HttpServletRequest request){
        RestResponse restResponse=null;
        try {
            String taskId = request.getParameter("taskId");
            String num = request.getParameter("num");
            //震级列表
            List<String> levelList = setEarthquakeMapService.queryInfluencefliedLevelList(taskId,num);
            //角度列表
            List<String> angleList = setEarthquakeMapService.queryInfluencefliedAngleList(taskId,num);
            //查询结果列表
            List<List<String>> resultList = new ArrayList<List<String>>();
            resultList.add(levelList);
            resultList.add(angleList);

            restResponse=RestResponse.succeed(resultList);

        }catch (Exception e){
            log.error("查询数据失败!",e);
            restResponse = RestResponse.fail("查询数据失败");
        }
        return restResponse;
    }

    /*
     * 根据taskId、震级、查询类型、序号总个数，按序号从小到大排序查询全部评估结果（未选择角度：最小值,最大值）
     */
    @GetMapping("/queryAllEvaluateResultTotalDetails")
    public RestResponse queryAllEvaluateResultTotalDetails(HttpServletRequest request){
        RestResponse restResponse=null;
        try {
            String taskId = request.getParameter("taskId");
            String level = request.getParameter("level");
            String type = request.getParameter("type");
            String lengthStr = request.getParameter("lengthStr");
            restResponse=RestResponse.succeed(setEarthquakeMapService.queryAllEvaluateResultTotalDetails(taskId,level,type,lengthStr));

        }catch (Exception e){
            log.error("查询数据失败!",e);
            restResponse = RestResponse.fail("查询数据失败");
        }
        return restResponse;
    }

    /*
     * 根据查询类型、taskId、震级、角度、序号、经度、纬度，查询评估结果（选择角度：1条数据）
     */
    @GetMapping("/queryEvaluateResultTotalDetailsByAngle")
    public RestResponse queryEvaluateResultTotalDetailsByAngle(HttpServletRequest request){
        RestResponse restResponse=null;
        try {
            String type = request.getParameter("type");
            String taskId = request.getParameter("taskId");
            String level = request.getParameter("level");
            String angle = request.getParameter("angle");
            String num = request.getParameter("num");
            String longitude = request.getParameter("longitude");
            String latitude = request.getParameter("latitude");
            restResponse=RestResponse.succeed(setEarthquakeMapService.queryEvaluateResultTotalDetailsByAngle(type,taskId,level,angle,num,longitude,latitude));

        }catch (Exception e){
            log.error("查询数据失败!",e);
            restResponse = RestResponse.fail("查询数据失败");
        }
        return restResponse;
    }

    /*
     * 根据taskId、震级、角度、序号、经度、纬度，按烈度从小到大排序查询地震影响场
     */
    @GetMapping("/queryInfluencefliedEllipse")
    public RestResponse queryInfluencefliedEllipse(HttpServletRequest request){
        RestResponse restResponse=null;
        try {
            String taskId = request.getParameter("taskId");
            String level = request.getParameter("level");
            String angle = request.getParameter("angle");
            String num = request.getParameter("num");
            String longitude = request.getParameter("longitude");
            String latitude = request.getParameter("latitude");
            restResponse=RestResponse.succeed(setEarthquakeMapService.queryInfluencefliedEllipse(taskId,level,angle,num,longitude,latitude));

        }catch (Exception e){
            log.error("查询数据失败!",e);
            restResponse = RestResponse.fail("查询数据失败");
        }
        return restResponse;
    }

    /**
     * 根据经纬度查询最近断层切线角度（与正东方向的夹角，[0,180]）
     */
    @GetMapping("/getClosestFaultageAngleByLonLat")
    public RestResponse getClosestFaultageAngleByLonLat(HttpServletRequest request){
        RestResponse restResponse = null;
        try{
            String lon = request.getParameter("lon");
            String lat = request.getParameter("lat");
            restResponse = RestResponse.succeed(setEarthquakeMapService.getClosestFaultageAngleByLonLat(lon,lat));
        }catch (Exception e){
            log.error("查询数据失败!",e);
            restResponse = RestResponse.fail("查询数据失败");
        }
        return restResponse;
    }

    /**
     * 根据全部经纬度字符串批量查询最近断层切线角度列表（与正东方向的夹角，[0,180]）
     */
    @PostMapping("/getClosestFaultageAngleListByLonLatsString")
    public RestResponse getClosestFaultageAngleListByLonLatsString(@RequestBody LonLatsStringQueryParams obj){
        RestResponse restResponse = null;
        try{
            String taskId = obj.getTaskId();//任务ID
            String lonlatsString = obj.getLonlatsString();//全部经纬度字符串
            restResponse = RestResponse.succeed(setEarthquakeMapService.getClosestFaultageAngleListByLonLatsString(lonlatsString));
        }catch (Exception e){
            log.error("查询数据失败!",e);
            restResponse = RestResponse.fail("查询数据失败");
        }
        return restResponse;
    }

    /*zyt修改--end*/
}
